<?php
class User{
    protected $main_table = 'data_user';		
	protected $main_id = 'user_id';
	protected $db = null;	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert user
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $rows ?  $this->db->getInsertId() : '';
		return $insert_id;	
	}
	

	/**
	* 	update user
	* 	@param array $data 
	* 	@param array or string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete user
	* 	@param array or string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}

	public function get_max_id(){
		$max = $this->db->getOne("SELECT MAX(".$this->main_id.") as max FROM ".$this->main_table)+1 ;
		return $max;	
	}

	/**
	* 	get user info
	* 	@param int $id 
	* 	@return array one row data
	*/

	public function get_data_row($id,$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}

	/**
	* 	get user array
	* 	@param array or string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='user_id',$limit='10',$field='user_id,user_name,user_check'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   
	
	
	public function check_answer($user,$question,$answer){
		$sql = "SELECT user_mail FROM ".$this->main_table." WHERE user_name='".inputSQL($user)."' AND user_question='".inputSQL($question)."' AND user_answer='".inputSQL($answer)."' ";
		return $this->db->getOne($sql);
	}
	/**
	* 	check user mail 
	* 	@param string $mail 
	* 	@return bool
	*/
/*	public function check_data_mail($mail){
		$pattern = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/i";
		if (preg_match($pattern,$mail)) {
			return true;
		} else {
			return false;
		}
	}*/
	
	/**
	* 	check user pwd 
	* 	@param int $id 
	*	@param string $password 
	* 	@return bool
	*/
	public function check_user_pwd($id,$pwd){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' AND user_pwd='".md5($pwd)."'";
		$result = $this->db->getRow($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}
	}
	
	public function md5pass($pass,$salt){
		return md5(md5($pass).$salt);	
	}
	/**
	* 	set news (hot display etc.)
	*	@param string $default 
	*	@param string $type 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function set_data($default='',$type,$where){
		if(!$type){
			my_jump('请指定更新类型');
		}
		$set = array();
		switch($type){
			case 'check':
				if($default=='1')
				$set = array('user_check'=>'0');
				else
				$set = array('user_check'=>'1');
			break;
			/*case 'bgadmin':
				if($default=='1')
				$set = array('user_bgadmin'=>'0');
				else
				$set = array('user_bgadmin'=>'1');
			break;*/
			default:
				my_jump('输入的参数有误');
			break;
		}
		$result = $this->db->update($this->main_table,$set,$where);
		return $result;
	}

	/**
	* 	set reg/edit checking code
	*	@param int $id
	* 	@return string code
	*/
	public function set_check_code($id){
		$code = random_code(8);
		$data = array('user_code'=>$code);
		$result = $this->update_data($this->main_table,$data,$this->main_id."='".intval($id)."'");
		if($result)
			return $code;
		else
			return false;
	}
	
	/**
	* 	check user reg info
	*	@param array data
	* 	@return bool
	*/
	public function check_reg_info($data){
		if(!$data['user_name']){
			return 'USER_NAME_NULL'	;
		}else{
			$pattern = "/^[a-zA-Z0-9_-]+$/i";
			if (!preg_match($pattern,$data['user_name'])) {
				return 'USER_NAME_ERROR';
			}
			$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_name='".$data['user_name']."'";
			if($this->db->getOne($sql)){
				return 'USER_NAME_EXIST';	
			}
		}
		if(!$data['user_pwd']){
			return 'USER_PWD_NULL'	;
		}else{
			if($data['user_pwd'] != $data['user_pwd2']){
				return 'USER_PWD_DIF';
			}
		}
		if(!$data['user_mail']){
			return 'USER_MAIL_NULL';
		}else{
			if(!check_mail($data['user_mail']))
				return 'USER_MAIL_ERROR';
			
			$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_mail='".$data['user_mail']."'";
			if($this->db->getOne($sql)){
				return 'USER_MAIL_EXIST';	
			}
		}
		return '1';										//没错误
	}
	
	/**
	* 	checking code
	*	@param int $id
	*	@param string $code
	* 	@return bool
	*/
	public function check_code($id,$code){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' AND user_code='".$code."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	}

	public function check_mail_exist($noid,$mail){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE ".$this->main_id."!='".intval($noid)."' AND user_mail='".$mail."' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return true;
		} else {
			return false;
		}	
		
	}

	public function search_user($username){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_name = '".$username."' ";
		$result = $this->db->getRow($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}	
		
	}
	
	public function search_phone($phone){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_phone = '".$phone."' ";
		$result = $this->db->getRow($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}	
		
	}
	
	public function get_user_info($field,$where){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where;
		$result = $this->db->getRow($sql);
		return $result;
	}

	

	public function check_by_sina_uid($sinauid){
		$sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_api_sina_id = '".$sinauid."' and user_api_sina_id>'0' ";
		$result = $this->db->getOne($sql);
		if ($result) {
			return $result;
		} else {
			return false;
		}
		
	}

    public function check_by_qq_uid($qquid){
        $sql = "SELECT ".$this->main_id." FROM ".$this->main_table." WHERE user_api_qq_id = '".$qquid."'";
        $result = $this->db->getOne($sql);
        if ($result) {
            return $result;
        } else {
            return false;
        }

    }
	
	//获取的手机归属地信息	
	public function get_phone_info($phone){
		$url = 'http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile='.$phone;		
		$xml = file_get_contents($url);
		$xml = iconv('gb2312', 'UTF-8', $xml);	
		preg_match( "/\<city\>(.*?)\<\/city\>/", $xml, $city);
		preg_match( "/\<province\>(.*?)\<\/province\>/", $xml, $province);
		preg_match( "/\<supplier\>(.*?)\<\/supplier\>/", $xml, $supplier);
		if($city[1] && $province[1]){			
			$sql = "SELECT id FROM data_city WHERE pid > '0' and name LIKE '".trim($city[1])."%'";
			$city_id = $this->db->getOne($sql);
			if(!$city_id){//没有查询到相应的信息跳出
				return $this->get_phone_info2($phone);//调用另外一个接口查询手机归属地
			}
			$sql = "SELECT id FROM data_city WHERE pid = '0' and name LIKE '".trim($province[1])."%'";
			$province_id = $this->db->getOne($sql);
			$user_update['user_phone_province'] = $province_id;
			$user_update['user_phone_city'] = $city_id ;
			$user_update['user_phone_supplier'] = trim($supplier[1]);			
			return	$user_update;		
			
		}else{
			return $this->get_phone_info2($phone);//调用另外一个接口查询手机归属地
		}
		
			
	}
	
	//获取的手机归属地信息2	
	public function get_phone_info2($phone){
		$url = 'http://shouji.7796.com/'.$phone;
		$handle = fopen ($url, "rb");
		do {
			$data = fread($handle, 1024);
			$data = iconv('gb2312', 'UTF-8', $data);
			$str = strstr($data,'<font color="red">');
			if($str){
				preg_match_all( "/\<font color=\"red\"\>(.*?)\<\/font\>/", $data, $area);//获取地区信息
				preg_match_all( "/\<li\>(.*?)\<\/li\>/", $data, $supplier);//获取供应商的信息
				if($supplier){					
					$supplier = substr($supplier[0][3],24,6);
				}else{//没有截取到页面的通信运行商信息
					fclose ($handle);
					return false;
				}
				
				if($area){
					$province =  trim($area[1][1],'<b>,</b>');
					$city =   trim($area[1][2],'<b>,</b>');	
					if($province && $city){			
						$sql = "SELECT id FROM data_city WHERE pid > '0' and  name LIKE '".$city."%'";
						$city_id = $this->db->getOne($sql);
						if(!$city_id){//没有查询到城市相应的信息
							fclose ($handle);
							return false;
						}
						$sql = "SELECT id FROM data_city WHERE pid = '0' and  name LIKE '".$province."%'";
						$province_id = $this->db->getOne($sql);
						if(!$province_id){//没有查询到省份相应的信息
							fclose ($handle);
							return false;
						}
						$user_update['user_phone_province'] = $province_id;
						$user_update['user_phone_city'] = $city_id ;
						$user_update['user_phone_supplier'] = $supplier;						
						fclose ($handle);						
						return	$user_update;		
						
					}else{//没有同时截取省份和城市的信息
						fclose ($handle);
						return false;
					}					
					
				}else{//没有截取到页面的地区信息
					fclose ($handle);
					return false;	
				}
					
			}
			
			if (strlen($data) == 0){
				fclose ($handle);
				return false;				
			}
		
		} while(true);
					
	}
	
	
	public function get_sales_top($limit){
		 $sql = "SELECT user_shop_title,user_sales,user_sales_money FROM ".$this->main_table." WHERE user_sales >0 ORDER BY user_sales_money DESC LIMIT ".$limit;
		 $data = $this->db->getAll($sql);
		 return $data;
			
	}	
	public function add_favorite($gid,$uid){
		$check = $this->db->getOne("select fav_id from data_favorite where fav_gid='".$gid."'");
		if($check)
		return 2;
		$insert = array('fav_uid'=>$uid,'fav_gid'=>$gid,'fav_create_time'=>date("Y-m-d H:i:s"));
		$rs =$this->db->insert("data_favorite", $insert);
		return $rs ? 1 : 0;
	}

}

?>